<?php
	ob_start();
	session_start();
	require("../lib/config.php");
	require("../lib/database.php");
	require("lib/nguoidung.php");
	if($_SESSION['ses_level'] != 1)
	{
		header("location:login.php");
		exit();
	}
	else
	{
		$id=$_SESSION['ses_userid'];
		$db = new libraries_user;
		$data = $db->getdata($id);
		$_SESSION['ses_name']=$data['ten'];
		$ten=$_SESSION['ses_name'];
		?>
		<input type="hidden" id="adminname" value="<?php echo $ten;?>" />
		<?php
	}
	ob_end_flush();
?>
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Live Chat - DASHBOAR</title>
	<link href="public/css/bootstrap.min.css" rel="stylesheet" />
	
	<!-- bootstrap JavaScript -->
	<script type="text/javascript" src="public/js/bootstrap.min.js"></script>
	<!--custom css-->
	<link rel="stylesheet" type="text/css" href="public/css/style.css">
	
<!--Client nodejs-->
	<script src="http://192.168.1.51:8001/socket.io/socket.io.js"></script>
	<script src="public/js/jquery.min.js"></script>
	<script>
		var socket = io.connect('http://localhost:8001');

		
		//Lay ten admin
		var adminname= $('#adminname').val();

		socket.on('connect', function (username){
			// tao ra su kien adduser gui den server
			socket.emit('adduser', ''+ adminname +'</strong>');
		});
		//nhan su kien updatuser tu server
		socket.on('updateusers', function (username,id)
		{
			id=socket.id;//
			$('#info').append('<li class="list-group-item' + id + '">' + username + '<span class="badge">' + id + '</span></li>');
			if (io.sockets.connected[id].disconnect()) {
			    $('#info').remove('.' + dtadmin.Ma + ' ');
			}
		});
		
		// Lang nghe, khi co nguoi chat thi chay su kien cap nhat lai toan bo khung chat
		socket.on('updatechat', function (username, id, data) 
		{
			//id=socket.socket.sessionid;//
			var dtadmin={Ma:' ' + id + ' ',Name: '' + username + '', Message: ' ' + data + ' '}
			$('#conversation').append('<b style="color:red;">'+ dtadmin.Name + ':</b>' + dtadmin.Ma + ':' + dtadmin.Message + '<br>'); 
		});
		// lắng nghe, bất cứ khi nào client phát ra 'updaterooms', đều cập nhật phòng khách hàng
		socket.on('updaterooms', function (rooms, current_room) 
		{
			$('#rooms').empty();
			$.each(rooms, function(key, value) 
			{
				if(value == current_room)
				{
					$('#rooms').append('<div>' + value + '</div>');
				}
				else
				{
					$('#rooms').append('<div><a href="#" onclick="switchRoom(\''+value+'\')">' + value + '</a></div>');
				}
			});
		});

		function switchRoom(room){
		socket.emit('switchRoom', room);
		}
		
		// on load of page
		$(function()
		{
			// Khi nguoi dung nhan nut gửi
			$('#datasend').click( function() {
				var message = $('#data').val();
				$('#data').val('');
				// bao cho server biet su kien send chat xay ra
				socket.emit('sendchat', message);
			});
			// Khi nhan phim enter
			$('#data').keypress(function(e) 
			{
				if(e.which == 13) 
				{
					$(this).blur();
					$('#datasend').focus().click();
				}
			});
		});
		</script>
<!--end nodejs-->	

</head>
<body>
	<div class="container">
		<div class="row">
			<div class="col-md-12">
				<h2>Live Chat - QUẢN LÝ</h2>
			</div>
		</div><!--/end row-->

		<div class="row">
			<div class="col-md-2"></div>

			<div class="col-md-8">
			<table data-toggle="table" data-url="data1.json" data-cache="false" data-height="100%" width="100%">
				<tbody>
					<tr>
						<td colspan="2">
							<div class="panel panel-primary">
								<div class="panel-heading">Chat với chúng tôi</div>
						</td>
					</tr>
							
					<tr>
							<td valign="top">
								<div class="profile" id="rooms">
								</div>
								<div class="list-customer" >
									<ul class="list-group" id="info">
									</ul>
								</div>
								Admin <strong id="admin_name"><?php echo $ten;?></strong>
							</td>
							<td>
								<div class="panel-body over" id="conversation">
									Lịch sử chat</br>
								</div>
								
								<div class="input-group">
									<input type="text" class="form-control" placeholder="Nhập nội dung cần gửi" id="data"></input>
									<span class="input-group-btn">
										<button class="btn btn-default" type="button" id="datasend">Send</button>
									</span>
								</div><!-- /input-group -->
							</td>
									
						</div><!--End .panel panel-primary-->
					</tr>
				</tbody>
			</table>
			</div>
			<!--end .col-md-8-->

			<div class="col-md-2"></div>

		</div><!--end row-->

		<div id="footer">
			<div class="row">
				<div class="col-md-12">
					Thực hiện bởi nhóm 11-12<br/>
				</div>
				
			</div>
			<div class="row">
				<div class="col-md-3"></div>
				<div class="col-md-9">
					<span class="label label-default">Nguyen van A</span>
					<span class="label label-primary">Nguyen van B</span>
					<span class="label label-success">Nguyen van C</span>
					<span class="label label-info">Nguyen van D</span>
					<span class="label label-warning">Nguyen van E</span>
					<span class="label label-danger">Nguyen van F</span>
				</div>
			</div>
		</div><!--End footer-->
	</div>
</body>
</html>